****************************************************************************************************************************
clear 
use "${output_stata}\reg_perf.dta" // Load the dataset

keep if entree == "external" | entree == "int_const" // Keep observations for internal vs external entries

gen log_eff_en = log(eff_3112_en) 
gen build = (entree == "int_const") // Create a binary variable for 'build' entries

*Fixed effects
egen dest_type = group(code_entry_num build) // Create a grouped variable by code_entry_num and build
egen orig_type = group(apgr_1 build) // Create a grouped variable by apgr_1 and build
egen year_type = group(year build) // Create a grouped variable by year and build
egen delta_type = group(delta_b build) // Create a grouped variable by delta_b and build
egen id_type = group(id build) // Create a grouped variable by id and build
egen orig_dest_delta = group(apgr_1 code_entry_num delta_b) // Create a grouped variable by apgr_1, code_entry_num, and delta_b

xtset id delta_b // Set panel data structure
gen g_eff = (eff_3112_en - L.eff_3112_en)/L.eff_3112_en // Generate growth rate of employment
winsor2 g_eff, replace cuts(1 99) // Winsorize g_eff at the 1st and 99th percentiles

drop q_HC2_* // Drop any variables starting with q_HC2_

* First regression - generate interaction terms
forval num = 1/8 {
    gen q_HC2_`num' = q_HC2 * (delta_b == `num') // Generate q_HC2_* for each delta_b value
    gen q_HC2_`num'_build = q_HC2_`num' * build // Interaction of q_HC2_* with build
}
gen q_HC2_0_build = q_HC2 // Baseline

eststo reg1: reghdfe g_eff q_HC2_1 q_HC2_2 q_HC2_3  q_HC2_4 /*
*/ q_HC2_5 q_HC2_6 q_HC2_7 q_HC2_8  /*
*/ q_HC2_0_build q_HC2_1_build q_HC2_2_build q_HC2_3_build q_HC2_4_build /*
*/ q_HC2_5_build q_HC2_6_build q_HC2_7_build q_HC2_8_build,   /*
*/ absorb(id code_entry_num apgr_1 year_entry delta_type) cluster(id delta_b) // Regression with absorbed fixed effects and clustered standard errors

drop q_HC2_* // Drop generated q_HC2_* variables

* Repeat for the second regression
forval num = 1/8 {
    gen q_HC2_`num'_build = q_HC2 * (delta_b == `num')
    gen q_HC2_`num' = q_HC2_`num' * build
}
gen q_HC2_0_build = q_HC2

eststo reg2: reghdfe g_eff q_HC2_1 q_HC2_2 q_HC2_3  q_HC2_4 /*
*/ q_HC2_5 q_HC2_6 q_HC2_7 q_HC2_8  /*
*/ q_HC2_0_build q_HC2_1_build q_HC2_2_build q_HC2_3_build q_HC2_4_build /*
*/ q_HC2_5_build q_HC2_6_build q_HC2_7_build q_HC2_8_build,   /*
*/ absorb(id code_entry_num apgr_1 year_entry delta_type) cluster(id delta_b)  

coefplot (reg2 , recast(connected) ci(95) lcolor(navy%60) mcolor(navy%60) msize(medsmall) lwidth(medthin) ciopts(recast(rcap) color(navy%60) fcolor(navy%60) lpattern(dash))  label("High HC Distance")) /*
*/ (reg1 , offset(0.1)  recast(connected) ci(95) lcolor(maroon%90) mcolor(maroon%90) msize(medsmall) lwidth(medthin) ciopts(recast(rcap) color(maroon%90) fcolor(maroon%90) lpattern(dash))  label("High HC Distance x Build")), /// 
    omitted drop(_cons q_HC2_1 q_HC2_2 q_HC2_3  q_HC2_4 /*
*/ q_HC2_5 q_HC2_6 q_HC2_7 q_HC2_8 q_HC2_8_build) /*
*/ order(q_HC2_0_build q_HC2_1_build q_HC2_2_build q_HC2_3_build /*
*/ q_HC2_4_build q_HC2_5_build q_HC2_6_build q_HC2_7_build) /* 
*/ rename(q_HC2_*_build= "") vertical yline(0,lpattern(shortdash) lcolor(grey)) title("Employment growth") /// 
    xlabel(1 "-4" 2 "-3" 3 "-2" 4 "-1" 5 "0" 6 "1" 7 "2" 8 "3" ,labsize(small) ) xsize(6) ysize(4) ylabel(,nogrid) xtitle("Year to Entry") ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white))  ytitle("") 
graph export "${export}\build_vs_buy_g_eff.pdf", replace // Export graph to PDF

***********************************************************

drop log_sales // Drop existing log_sales variable
gen log_sales = log(s001) // Generate log-transformed sales
winsor2 log_sales, replace  cuts(0 99) // Winsorize log_sales at 0 and 99 percentiles
drop q_HC2_* // Drop any existing q_HC2_* variables

* First regression for log_sales
forval num = 1/8 {
    gen q_HC2_`num' = q_HC2 * (delta_b == `num')
    gen q_HC2_`num'_build = q_HC2_`num' * build
}
gen q_HC2_0_build = q_HC2

eststo reg1: reghdfe log_sales q_HC2_4 /*
*/ q_HC2_5 q_HC2_6 q_HC2_7 q_HC2_8  /*
*/ q_HC2_4_build /*
*/ q_HC2_5_build q_HC2_6_build q_HC2_7_build q_HC2_8_build if delta>=0,   /*
*/ absorb(code_entry_num apgr_1 year_entry delta_type) cluster(id delta_b)

drop q_HC2_* 

forval num = 1/8 {
    gen q_HC2_`num'_build = q_HC2 * (delta_b == `num')
    gen q_HC2_`num' = q_HC2_`num' * build
}
gen q_HC2_0_build = q_HC2

eststo reg2: reghdfe log_sales q_HC2_4 /*
*/ q_HC2_5 q_HC2_6 q_HC2_7 q_HC2_8  /*
*/ q_HC2_4_build /*
*/ q_HC2_5_build q_HC2_6_build q_HC2_7_build q_HC2_8_build if delta>=0,   /*
*/ absorb(code_entry_num apgr_1 year_entry delta_type) cluster(id delta_b) 

coefplot (reg2 , recast(connected) ci(95) lcolor(navy%60) mcolor(navy%60) msize(medsmall) lwidth(medthin) ciopts(recast(rcap) color(navy%60) fcolor(navy%60) lpattern(dash))  label("High HC Distance")) /*
*/ (reg1 , offset(0.1) recast(connected) ci(95) lcolor(maroon%90) mcolor(maroon%90) msize(medsmall) lwidth(medthin) ciopts(recast(rcap) color(maroon%90) fcolor(maroon%90) lpattern(dash))  label("High HC Distance x Build")), /// 
    omitted drop(_cons q_HC2_4 /*
*/ q_HC2_5 q_HC2_6 q_HC2_7 q_HC2_8 q_HC2_8_build) /*
*/ order( /*
*/ q_HC2_4_build q_HC2_5_build q_HC2_6_build q_HC2_7_build) /* 
*/ rename(q_HC2_*_build= "") vertical yline(0,lpattern(shortdash) lcolor(grey)) title("log(Sales)") ///
    xlabel(1 "0" 2 "1" 3 "2" 4 "3" ,labsize(small) ) xsize(6) ysize(4) ylabel(,nogrid) xtitle("Year to Entry") ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white))  ytitle("") 
graph export "${export}\build_vs_buy_sales.pdf", replace // Export graph to PDF
